//
//  GoodsActive.swift
//  eemall
//
//  Created by zhuxietong on 2018/12/24.
//  Copyright © 2018 tianrun. All rights reserved.
//

import UIKit
import Eelay
import AsyncDisplayKit
import soapp



public struct GoodsActive{
    
    public class Cell: JoCellNode {
        let imgV = JoImageNode()
        let nameL = JoTextNode()
        let infoL = JoTextNode()
        
        public override func specGetter() -> _Spec {
            let spec:_Spec = .stack(["h|10",[10,AppTheme.edge],_nowrap], childs: [
                imgV[100,"100"],
                .stack(["v|8",&-.stretch,_shrink(1),_justfy(.space_between)], childs: [
                    nameL[_grow(1)],
                    infoL[_grow(1)],
                    ])
                ])
            
            return spec
        }
        
        public override func mo_model_reload() {
            imgV.img_url = mo.model["img",""]
            nameL.attributedText = mo.model["title",""].ui.font14.cl_444.astring
            nameL.textNode.maximumNumberOfLines = 2
            
            infoL.attributedText = mo.model["subTitle",""].ui.font13.cl_888.astring
            infoL.textNode.maximumNumberOfLines = 2
        }
    }
    
    public class HeaderConfig: ASColDataSource {
        
        public override func collectionNode(_ collectionNode: ASCollectionNode, nodeForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> ASCellNode {
            if indexPath.section == 1 && indexPath.row == 0
            {
                let cell = BarCell()
                cell.mo.model = NSMutableDictionary()
                cell.delegate = delegate as? JoTableCellDelegate
                return cell
            }
            return ASCellNode()
        }
        
        public override func collectionNode(_ collectionNode: ASCollectionNode, numberOfItemsInSection section: Int) -> Int {
            return super.collectionNode(collectionNode, numberOfItemsInSection: section)
        }
    }
    
    
    public class BarCell: JoCellNode,EeActionDelegate{
        
        class Kit: JoTouchNode {
            let nameL = JoTextNode()
            let indicatorV = ASDisplayNode()
            
            override func specGetter() -> _Spec {
                let w:CGFloat = mo.model[float:"width",Swidth] - TP.separatorHeight
                return .stack(["v|0",&-.stretch,w,"40",_justfy(.space_between)], childs: [
                    .inset([_grow(1)], [2,0,0,0], child: nameL[_grow(1),_shrink(1)]),
                    indicatorV["2"]
                    ])
            }
            
            var selected:Bool = false{
                didSet{
                    
                    if selected{
                        indicatorV.backgroundColor = UIColor.orange
                        self.mo.model[FK.selected] = FK.YES
                        
                    }
                    else{
                        indicatorV.backgroundColor = UIColor.table_bg_color
                        self.mo.model[FK.selected] = FK.NO
                        
                    }
                }
            }
            
            override func mo_model_reload() {
                nameL.attributedText = mo.model["name",""].ui.font12.center.cl_222.astring
                let tag = mo.model[FK.selected,FK.NO]
                if tag == FK.YES
                {
                    self.selected = true
                }
                else{
                    self.selected = false
                }
                //                print("====UUU|\(mo.model)")
            }
        }
        
        let list = JoRuleNode<Kit>()
        
        public override func addLayoutRules() {
            list.delegate = self
            list.space = 0
            //            list.containerConfig = {
            //                stack in
            //                stack.flexWrap = .noWrap
            //            }
        }
        
        public override func mo_model_reload() {
            //            正在热拍 即将开始 历史往期
            let w:CGFloat = (Swidth/3.0)
            let infos = [
                "list":[
                    ["name":"正在热拍".loc,"width":w,FK.selected:FK.YES],
                    ["name":"即将开始".loc,"width":w],
                    ["name":"历史往期".loc,"width":w]
                ]
                ].mutable_dictionary
            list.mo.model = infos
            backgroundColor = .white
        }
        
        public func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
            
            
            list.nodes.forEach {[weak self] (kit) in
                guard let ws = self else{return}
                if kit.mo.model === data
                {
                    kit.selected = true
                    ws.active(ID: "changeTypes", object: data)
                }else
                {
                    kit.selected = false
                }
            }
        }
        
        public override func specGetter() -> _Spec {
            return .inset([], .zero, child: list.Spec)
        }
    }
    
    
    public class TopKit: LoopKitNode {
        
        public override func buttonAction() {
            AppLink.open(link: mo.model["link",""], at: mo.controller)
            //            print("___+++++D||\(mo.model)")
        }
//        override func mo_model_reload() {
//            imgNode.img_url = mo.model["thumb",""]
//        }
//        override class var height:CGFloat{
//            get{
//                return 174.co
//            }
//        }
        
        public override func mo_model_reload() {
            imgNode.img_url = mo.model.possible("img","thumb", default: "")
        }
        public override class var height:CGFloat{
            get{
                return 200.co
            }
        }
    }
}



